/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship.ship;

import battleship.board.LocationType;
import java.util.HashMap;
import java.util.Map;
import util.MsgPrinter;

/**
 *
 * @author ali
 */
public abstract class AbstractShip implements ShipType {

    protected String shipName = "genericShipName";
    protected int MAX_HITS_POSSIBLE;
    protected int actualHits;
    protected Map<LocationType, Boolean> shipLocations = new HashMap<LocationType, Boolean>();
    protected int startX, startY, endX, endY;
    protected DirectionEnum dir;

    public int getMAX_HITS_POSSIBLE() {
        return MAX_HITS_POSSIBLE;
    }

    public DirectionEnum getDir() {
        return dir;
    }
    
    AbstractShip(int maxHitsPossible, int startX, int startY, DirectionEnum dir, String nameShip) {
        this.startX = startX;
        this.startY = startY;
        this.dir = dir;
        this.shipName = nameShip;

        if (dir.equals(DirectionEnum.HORIZONTAL)) {
            this.endX = this.endX + maxHitsPossible;
            this.endY = this.startY;
        } else {
            this.endX = this.startX;
            this.endY = this.endY + maxHitsPossible;
        }
        this.MAX_HITS_POSSIBLE = maxHitsPossible;
    }

    public void initShipLocations(LocationType start, LocationType end) {
    }

    public Map<LocationType, Boolean> getHits() {
        return shipLocations;
    }

    public boolean attemptHit(int x, int y) {
        boolean isHit = false;
        if (!isSunk()) {
            if (dir.equals(DirectionEnum.HORIZONTAL)) {
                if (startY == y && x >= startX && x <= endX) {
                    actualHits = actualHits + 1;
                    isHit = true;
                }
            } else {
                if (startX == x && y >= startY && y <= endY) {
                    actualHits = actualHits + 1;
                    isHit = true;
                }
            }
            if (isHit) {
                MsgPrinter.print(toString() + " ship hit. ");
                MsgPrinter.print("Hit stats : " + Integer.toString(actualHits) + " / " + Integer.toString(MAX_HITS_POSSIBLE));
            }
        } else {
            MsgPrinter.print(toString() + " has alrady sunk!");
        }
        return isHit;
    }

    @Override
    public String toString() {
        return shipName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AbstractShip other = (AbstractShip) obj;
        if ((this.shipName == null) ? (other.shipName != null) : !this.shipName.equals(other.shipName)) {
            return false;
        }
        return true;
    }

    public boolean isSunk() {
        boolean isSunk = false;
        if (actualHits >= MAX_HITS_POSSIBLE) {
            isSunk = true;
        }
        return isSunk;
    }

    @Override
    public int hashCode() {
        return shipName.hashCode();
    }

}
